/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package xsystems.propagationModel;

import java.io.Serializable;
import static java.lang.Math.PI;
import static java.lang.Math.abs;
import static java.lang.Math.acos;
import static java.lang.Math.cos;
import static java.lang.Math.sin;
import java.util.ArrayList;
import xsystems.parserFile.Data;

public class ImportedModel extends PropagationModel implements Serializable {

    //Pontos medidos
    private ArrayList<Data> data;

    //Posição do transmissor
    private double latitudeTrans;
    private double longitudeTrans;

    public ImportedModel(String nomeMedodo, double latitudeTrans, double longitudeTrans, ArrayList<Data> data) {
        super(nomeMedodo);
        this.latitudeTrans = latitudeTrans;
        this.longitudeTrans = longitudeTrans;
        this.data = data;

    }

    public ImportedModel(String nomeMedodo, ArrayList<Data> data) {
        super(nomeMedodo);
        this.latitudeTrans = data.get(0).getLatitude();
        this.longitudeTrans = data.get(0).getLongitude();
        this.data = data;

    }

    private double calculaDistancia(Data b) {

        double la1 = latitudeTrans;
        double lo1 = longitudeTrans;
        double la2 = b.getLatitude();
        double lo2 = b.getLongitude();

        double d = ((acos(
                cos((90 - la1) * PI / 180)
                * cos((90 - la2) * PI / 180)
                + sin((90 - la1) * PI / 180)
                * sin((90 - la2) * PI / 180)
                * cos(abs(((360 + lo1) * PI / 180) - ((360 + lo2) * PI / 180)))
        )) * 6371.004) * 1000;

        return d;
    }

    @Override
    public ArrayList<Point> getPoints() {
        ArrayList<Point> pontos = new ArrayList<>();
        double med = 0;
        int k = 0;

        for (int i = this.data.size()/2; i <this.data.size() ; i++) {
        //for (int i = 0; i < this.data.size() / 2; i++) {
            Data get = data.get(i);

            //faz uma media entre os k pontos
            if (k == 10) {
                double d = calculaDistancia(get);
                if (d < 500) {
                    pontos.add(new Point(d, med / k));
                }
                //pontos.add(new Point(d, get.getAmplitude()));
                med = 0;
                k = 0;
            } else {
                med = med + get.getAmplitude();
            }
            k++;
        }

        return pontos;
    }

    public void setTransmissor(double latitudeTrans, double longitudeTrans) {
        this.latitudeTrans = latitudeTrans;
        this.longitudeTrans = longitudeTrans;
    }

    public double getLatitudeTrans() {
        return latitudeTrans;
    }

    public double getLongitudeTrans() {
        return longitudeTrans;
    }

    public void setLatitudeTrans(double latitudeTrans) {
        this.latitudeTrans = latitudeTrans;
    }

    public void setLongitudeTrans(double longitudeTrans) {
        this.longitudeTrans = longitudeTrans;
    }

}
